//
//  DPTubeNetwork.m
//  NetworkStatus
//
//  Created by Joel Parsons on 21/11/2011.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DPTubeNetwork.h"

@implementation DPTubeNetwork

@synthesize currentTubeLine, tubeLineArray;

-(void)dealloc{
    [currentTubeLine release];
    self.tubeLineArray = nil;
    [super dealloc];
}

-(NSArray *)getTubeLineStatus{
    tubeLineArray = [[NSMutableArray alloc] init];
    
    NSURL * dataURL = [NSURL URLWithString:@"http://joelparsons.net/networkStatus.xml"];
    
    NSXMLParser * XMLParser = [[NSXMLParser alloc] initWithContentsOfURL:dataURL];
    XMLParser.delegate = self;
    
    BOOL success = [XMLParser parse];
    
    if (success) {
        return [tubeLineArray autorelease];
        tubeLineArray = nil;
    }
    
    return [NSArray array];
}


//an example of the XML we are going to parse is included in the project and called file.xml

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    
    if ([elementName isEqualToString:@"LineStatus"]) {
        self.currentTubeLine = [[DPTubeLine alloc] init];
        NSString * statusDetails = [attributeDict objectForKey:@"StatusDetails"];
        self.currentTubeLine.statusDetails = statusDetails;
    }
    
    if ([elementName isEqualToString:@"Line"]) {
        NSString * lineName = [attributeDict objectForKey:@"Name"];
        self.currentTubeLine.name = lineName;
    }
    
    if ([elementName isEqualToString:@"Status"]) {
        NSString * status = [attributeDict objectForKey:@"Description"];
        self.currentTubeLine.statusCode = status;
    }    
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    
    if ([elementName isEqualToString:@"LineStatus"]) {
        [self.tubeLineArray addObject:self.currentTubeLine];
        [currentTubeLine release];
        currentTubeLine = nil;
    }
    
}




















@end
